<?php

namespace app\user\logic;

use app\model\Disease;
use app\model\DiseaseCategory;

class DiseaseLogic
{
    protected Disease $model;
    protected int $factory_id;

    public function __construct()
    {
        $this->model = new Disease();
        $this->factory_id = request()->user_info['factory_id'];
    }

    /**
     * @Description 列表
     * @author: zj
     * @param array $param
     * @time 2024/03/20 18:20:57
     * @return mixed
     */
    public function getList(array $param)
    {
        $query = $this->model->alias('d')
            ->join('factory f','d.factory_id = f.id','left')
            ->join('pig p','p.out_id = d.out_pig_id','left')
            ->join('fence fe','fe.out_id = p.out_fence_id','left')
            ->join('barn b','fe.out_barn_id = b.out_id','left')
            ->join('disease_category dc','dc.id = d.cate_id','left')
            ->field('d.out_id as id,d.factory_id,f.name as factory,d.out_pig_id as pig_id,p.tag_address,p.out_fence_id as fence_id,fe.name as fence,fe.out_barn_id as barn_id,b.name as barn,d.cate_id,dc.name as disease_category,d.disease_name,d.item_name,d.remark,d.record_time,d.create_time')
            ->where('d.factory_id',$this->factory_id)
            ->where('d.delete_time',$this->model::IS_DELETE_NO);
        if ($param['barn_id']) {
            $query->where('fe.out_barn_id',$param['barn_id']);
        }
        if ($param['fence_id']) {
            $query->where('p.out_fence_id',$param['fence_id']);
        }
        if ($param['cate_id']) {
            $query->where('d.cate_id',$param['cate_id']);
        }
        if ($param['record_time_start']) {
            $query->where('d.record_time','>=',strtotime($param['record_time_start']));
        }
        if ($param['record_time_end']) {
            $query->where('d.record_time','<=',strtotime($param['record_time_end']));
        }

        return $query->order('d.out_id DESC')
            ->paginate([
                'list_rows' => $param['page_size'],
                'page' => $param['page']
            ])
            ->toArray();
    }

    /**
     * @Description 统计
     * @author: zj
     * @param array $param
     * @time 2024/03/28 16:20:57
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function statistic(array $param)
    {
        $cateModel = new DiseaseCategory();
        $category = $cateModel->field('id,name')
            ->where('delete_time',$cateModel::IS_DELETE_NO)
            ->select()
            ->toArray();
        if (!$category) {
            return [];
        }

        $data = [];
        foreach ($category as $value) {
            $query = $this->model->alias('d')
                ->join('pig p','p.out_id = d.out_pig_id','left')
                ->join('fence fe','fe.out_id = p.out_fence_id','left')
                ->where('d.factory_id',$this->factory_id)
                ->where('d.cate_id',$value['id'])
                ->where('d.delete_time',$this->model::IS_DELETE_NO);
            if ($param['barn_id']) {
                $query->where('fe.out_barn_id',$param['barn_id']);
            }
            if ($param['fence_id']) {
                $query->where('p.out_fence_id',$param['fence_id']);
            }
            if ($param['record_time_start']) {
                $query->where('d.record_time','>=',strtotime($param['record_time_start']));
            }
            if ($param['record_time_end']) {
                $query->where('d.record_time','<=',strtotime($param['record_time_end']));
            }
            $count = $query->count() ?: 0;

            $data[] = [
                'category' => $value['name'],
                'count' => $count
            ];
        }

        return $data;
    }
}